package com.dcs.cn.cart.controller;


import cn.com.dcsgo.aspect.ApiOperationLog;
import com.dcs.cn.cart.domain.dto.CartFormDTO;
import com.dcs.cn.cart.domain.po.Cart;
import cn.com.dcsgo.api.dto.CartVO;
import com.dcs.cn.cart.service.ICartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "购物车相关接口")
@RestController
@RequestMapping("/carts")
@RequiredArgsConstructor
public class CartController {
    private final ICartService cartService;

    @ApiOperation("添加商品到购物车")
    @PostMapping
    @ApiOperationLog(description = "添加商品到购物车")
    public void addItemCart(@Valid @RequestBody CartFormDTO cartFormDTO){
        cartService.addItemCart(cartFormDTO);
    }

    @ApiOperation("更新购物车数据")
    @PutMapping
    @ApiOperationLog(description = "更新购物车数据")
    public void updateCart(@RequestBody Cart cart){
        cartService.updateById(cart);
    }

    @ApiOperation("删除购物车中商品")
    @DeleteMapping("{id}")
    @ApiOperationLog(description = "删除购物车中商品")
    public void deleteCartItem(@Param ("购物车条目id")@PathVariable("id") Long id){
        cartService.removeById(id);
    }

    @ApiOperation("查询购物车列表")
    @GetMapping("/{userId}")
    @ApiOperationLog(description = "查询购物车列表")
    public List<CartVO> queryMyCarts(@PathVariable Long userId){
        return cartService.queryMyCarts(userId);
    }
    @ApiOperation("批量删除购物车中商品")
    @ApiImplicitParam(name = "ids", value = "购物车条目id集合")
    @DeleteMapping
    @ApiOperationLog(description = "批量删除购物车中商品")
    public void deleteCartItemByIds(@RequestParam("ids") List<Long> ids, @RequestParam("userId") Long userId){
        cartService.removeByItemIds(ids,userId);
    }
}
